/**
 * 许可证删除接口测试用例
 *
 * Created by snowingsea on 2020/02/25.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');

const { statusKind } = require('./../../../../phoenix/models/license');

module.exports = describe('/DELETE license.json', function () {
  const url = '/license.json';
  const administrator = new Administrator();

  common.should.be.user(uri, url, 'delete');
  common.should.be.administrator(uri, url, 'delete');

  // 正常删除
  it('it should DELETE a license', function (done) {
    administrator.replaceLicense(statusKind.normal)
      .then(() => administrator.login())
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询
            chai.request(uri)
              .get(url)
              .set('Authorization', `Bearer ${administrator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              });
          });
      })
      .catch(done);
  });

  // 删除不存在的许可证
  it('it should fail to delete a license when the license is not exists', function (done) {
    administrator.replaceLicense(statusKind.normal)
      .then(() => administrator.removeLicense())
      .then(() => administrator.login())
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
